<?php

namespace app\utils;
use Qcloud\Cos\Client as QClient;

class QcloudCosUpload
{
    private $config;
    private $bucket;
    public function __construct()
    {
        $this->config = [
            'region' => 'ap-chengdu',
            'credentials' => [
                'appId' => '1252518172',
                'secretId' => 'AKIDnRvC6mCamQIuKUMGo9QUJzZuc5MuBNK3',
                'secretKey' => 'e6PabPyLySggWP2PaGfsreqLepqb7XQ2'
            ]
        ];
        $this->bucket = 'douwuku-1252518172';
    }

    //腾讯云 上传文件
    public function upload($type =1,$filename, $realPath)
    {
        $cosClient = new QClient($this->config);
        try {
            // 1 二进制 2 base64_decode后的内容($realPath)
            if ($type == 1) {
                $cosClient->upload($this->bucket, $filename, fopen($realPath, 'rb'));
            } else {
                $cosClient->upload($this->bucket, $filename, $realPath);
            }
        } catch (\Exception $e) {
            Log::info($e->getMessage());
            throw new \Exception($e->getMessage());
        }
    }
}